// fast4j - Fast & Agile Service Tools for Java
// Copyright (C) 2007 Alexandre ROMAN
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

// $Id: ApplicationDescriptor.java 51 2007-04-08 12:37:12Z alexandre.roman $

package fast4j.ace;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * Application descriptor, describing an application to create. This class is
 * used by an {@link ApplicationCreator} to generate an application.
 * @author alexandre.roman
 */
public class ApplicationDescriptor {
    private static final Pattern ID_PATTERN = Pattern
            .compile("[a-zA-Z]+[a-zA-Z0-9]*");
    private String id;
    private String name;
    private String version;
    private String rootPackage;
    private Map<String, String> properties = new HashMap<String, String>();
    
    /**
     * Application identifier. This identifier should only contain letters and
     * digits, and start with a letter.
     */
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Map<String, String> getProperties() {
        return properties;
    }
    
    public void setProperties(Map<String, String> properties) {
        this.properties = properties;
    }
    
    public String getVersion() {
        return version;
    }
    
    public void setVersion(String version) {
        this.version = version;
    }
    
    public String getRootPackage() {
        return rootPackage;
    }
    
    public void setRootPackage(String rootPackage) {
        this.rootPackage = rootPackage;
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result
                + ((properties == null) ? 0 : properties.hashCode());
        result = prime * result
                + ((rootPackage == null) ? 0 : rootPackage.hashCode());
        result = prime * result + ((version == null) ? 0 : version.hashCode());
        return result;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final ApplicationDescriptor other = (ApplicationDescriptor) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (properties == null) {
            if (other.properties != null)
                return false;
        } else if (!properties.equals(other.properties))
            return false;
        if (rootPackage == null) {
            if (other.rootPackage != null)
                return false;
        } else if (!rootPackage.equals(other.rootPackage))
            return false;
        if (version == null) {
            if (other.version != null)
                return false;
        } else if (!version.equals(other.version))
            return false;
        return true;
    }
    
    /**
     * Validate values.
     * @throws IllegalStateException if any value is wrong
     */
    public void validate() {
        if (StringUtils.isBlank(id)) {
            throw new IllegalStateException("Missing application descriptor id");
        }
        
        final Matcher idMatcher = ID_PATTERN.matcher(id);
        if (!idMatcher.matches()) {
            throw new IllegalStateException(
                    "Invalid application descriptor id: " + id);
        }
        
        if (StringUtils.isBlank(name)) {
            setName(id);
        }
        if (StringUtils.isBlank(version)) {
            setVersion("0.1");
        }
        if (StringUtils.isBlank(rootPackage)) {
            setRootPackage("id");
        }
    }
}
